﻿using System;
using System.Collections.Generic;
using System.Web.Mvc.Ajax;
using System.Web.Routing;
using System.Xml.XPath;

namespace Xmlt.ViewEngine
{
    internal static class XsltMVCHelper
    {
        internal static bool ParseRouteValues(object routeValues, out RouteValueDictionary routeValuesDictionary)
        {
            routeValuesDictionary = null;
            if (routeValues is string)
            {
                return false;
            }
            else if (routeValues is XPathNodeIterator)
            {
                routeValuesDictionary = new RouteValueDictionary();
                XPathNodeIterator iterator = routeValues as XPathNodeIterator;
                while (iterator.MoveNext())
                {
                    string name = iterator.Current.Name;
                    string value = iterator.Current.Value;
                    routeValuesDictionary.Add(name, value);
                }
                return true;
            }
            else if (routeValues is XPathNavigator)
            {
                routeValuesDictionary = new RouteValueDictionary();
                XPathNavigator navigator = routeValues as XPathNavigator;
                navigator = navigator.Clone();
                if (navigator.MoveToFirstChild())
                {
                    do
                    {
                        string name = navigator.Name;
                        string value = navigator.Value;
                        routeValuesDictionary.Add(name, value);
                    } while (navigator.MoveToNext());
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        internal static bool ParseHtmlAttributes(object unknown, out IDictionary<string, object> htmlAttributes)
        {
            htmlAttributes = null;
            if (unknown is string)
            {
                return false;
            }
            else if (unknown is XPathNodeIterator)
            {
                htmlAttributes = new Dictionary<string, object>();
                XPathNodeIterator iterator = unknown as XPathNodeIterator;
                while (iterator.MoveNext())
                {
                    string name = iterator.Current.Name;
                    string value = iterator.Current.Value;
                    htmlAttributes.Add(name, value);
                }
                return true;
            }
            else if (unknown is XPathNavigator)
            {
                htmlAttributes = new Dictionary<string, object>();
                XPathNavigator navigator = unknown as XPathNavigator;
                navigator = navigator.Clone();
                if (navigator.MoveToFirstChild())
                {
                    do
                    {
                        string name = navigator.Name;
                        string value = navigator.Value;
                        htmlAttributes.Add(name, value);
                    } while (navigator.MoveToNext());
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        internal static bool ParseAjaxOptions(object unknown, out AjaxOptions ajaxOptions)
        {
            ajaxOptions = null;
            if (unknown is string)
            {
                return false;
            }
            else if (unknown is XPathNodeIterator)
            {
                ajaxOptions = new AjaxOptions();
                XPathNodeIterator iterator = unknown as XPathNodeIterator;
                while (iterator.MoveNext())
                {
                    string name = iterator.Current.Name;
                    string value = iterator.Current.Value;
                    switch (name)
                    {
                        case "UpdateTargetId":
                            ajaxOptions.UpdateTargetId = value;
                            break;
                        case "OnComplete":
                            ajaxOptions.OnComplete = value;
                            break;
                        case "OnBegin":
                            ajaxOptions.OnBegin = value;
                            break;
                        case "HttpMethod":
                            ajaxOptions.HttpMethod = value;
                            break;
                        case "Confirm":
                            ajaxOptions.Confirm = value;
                            break;
                        case "InsertionMode":
                            switch (value)
                            {
                                case "InsertAfter":
                                    ajaxOptions.InsertionMode = InsertionMode.InsertAfter;
                                    break;
                                case "InsertBefore":
                                    ajaxOptions.InsertionMode = InsertionMode.InsertBefore;
                                    break;
                                case "Replace":
                                    ajaxOptions.InsertionMode = InsertionMode.Replace;
                                    break;
                                default:
                                    throw new ArgumentException("InsertionMode");
                            }
                            break;
                        case "LoadingElementId":
                            ajaxOptions.LoadingElementId = value;
                            break;
                        case "OnFailure":
                            ajaxOptions.OnFailure = value;
                            break;
                        case "OnSuccess":
                            ajaxOptions.OnSuccess = value;
                            break;
                        case "Url":
                            ajaxOptions.Url = value;
                            break;
                    }
                }
                return true;
            }
            else if (unknown is XPathNavigator)
            {
                ajaxOptions = new AjaxOptions();
                XPathNavigator navigator = unknown as XPathNavigator;
                navigator = navigator.Clone();
                if (navigator.MoveToFirstChild())
                {
                    do
                    {
                        string name = navigator.Name;
                        string value = navigator.Value;
                        switch (name)
                        {
                            case "UpdateTargetId":
                                ajaxOptions.UpdateTargetId = value;
                                break;
                            case "OnComplete":
                                ajaxOptions.OnComplete = value;
                                break;
                            case "OnBegin":
                                ajaxOptions.OnBegin = value;
                                break;
                            case "HttpMethod":
                                ajaxOptions.HttpMethod = value;
                                break;
                            case "Confirm":
                                ajaxOptions.Confirm = value;
                                break;
                            case "InsertionMode":
                                switch (value)
                                {
                                    case "InsertAfter":
                                        ajaxOptions.InsertionMode = InsertionMode.InsertAfter;
                                        break;
                                    case "InsertBefore":
                                        ajaxOptions.InsertionMode = InsertionMode.InsertBefore;
                                        break;
                                    case "Replace":
                                        ajaxOptions.InsertionMode = InsertionMode.Replace;
                                        break;
                                    default:
                                        throw new ArgumentException("InsertionMode");
                                }
                                break;
                            case "LoadingElementId":
                                ajaxOptions.LoadingElementId = value;
                                break;
                            case "OnFailure":
                                ajaxOptions.OnFailure = value;
                                break;
                            case "OnSuccess":
                                ajaxOptions.OnSuccess = value;
                                break;
                            case "Url":
                                ajaxOptions.Url = value;
                                break;
                        }
                    } while (navigator.MoveToNext());
                }
                return true;
            }
            else
            {
                return false;
            }
        }

    }

}
